package com.trw.trp.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import com.trw.trp.bean.FileBean;
import com.trw.trp.bean.PropertyBean;
import com.trw.trp.dao.DaoFactory;

import com.trw.trp.msg.ResultMessage;
import com.utuan.log.Log;
import com.utuan.log.LogFactory;

public class FileUploadAction extends AbstractAction {

	private static Log log = LogFactory.getLog(FileUploadAction.class);

	private static String FILE_DIR;
	static {
		PropertyBean prop = DaoFactory.getSystemSettingDao().getProperty(5);
		if (prop != null) {
			FILE_DIR = prop.getValue();
		}
	}

	private File uploadFile;
	private String orderNumber;
	private String type;
	private String fileName;
	private String isSingle;
	private InputStream inputStream;

	public String getIsSingle() {
		return isSingle;
	}

	public void setIsSingle(String isSingle) {
		this.isSingle = isSingle;
	}

	private InputStream fileInputStream;

	public InputStream getFileInputStream() {
		return fileInputStream;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public File getUploadFile() {
		return uploadFile;
	}

	public void setUploadFile(File uploadFile) {
		this.uploadFile = uploadFile;
	}

	public String getOrderNumber() {
		return orderNumber;
	}

	public void setOrderNumber(String orderNumber) {
		this.orderNumber = orderNumber;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public void upload() throws IOException {
		boolean flag = false;
		if (uploadFile != null) {
			try {
				if (fileName.contains(File.separator)) {
					fileName = fileName.substring(fileName
							.lastIndexOf(File.separator) + 1);
				}
				StringBuilder sb = new StringBuilder(FILE_DIR);
				File tmpFile = new File(sb.toString());
				if (!tmpFile.exists()) {
					tmpFile.mkdir();
				}
				sb.append(File.separator).append(orderNumber);
				tmpFile = new File(sb.toString());
				if (!tmpFile.exists()) {
					tmpFile.mkdir();
				}
				sb.append(File.separator).append(type);
				tmpFile = new File(sb.toString());
				if (!tmpFile.exists()) {
					tmpFile.mkdir();
				}
				if (Boolean.parseBoolean(isSingle)) {
					tmpFile = new File(sb.toString());
					if (tmpFile.exists()) {
						File[] files = tmpFile.listFiles();
						if (files != null && files.length > 0) {
							for (File file : files) {
								file.setWritable(true);
								file.delete();
							}
						}
					}
				}
				sb.append(File.separator).append(fileName).toString();
				tmpFile = new File(sb.toString());
				byte[] buffer = new byte[1024];
				int bytesRead = 0;

				OutputStream os = new FileOutputStream(tmpFile);
				InputStream is = new FileInputStream(uploadFile);
				while ((bytesRead = is.read(buffer)) > 0) {
					os.write(buffer, 0, bytesRead);
				}
				os.flush();
				os.close();
				is.close();
				flag = true;
			} catch (Exception e) {
				log.warn(e);
			}
		}
		ResultMessage ret = new ResultMessage();
		ret.setState(flag ? ResultMessage.SUCCESS : ResultMessage.FAILED);
		writeJSON4Form(flag, ret);
	}

	public void getFiles() throws IOException {
		String[] fileNames = getFiles(orderNumber, type);
		List<FileBean> fileList = new ArrayList<FileBean>();
		for (String fileName : fileNames) {
			FileBean bean = new FileBean();
			bean.setFileName(fileName);
			fileList.add(bean);
		}
		writeJSON(fileList);
	}

	public static String[] getFiles(String orderNumber, String type) {
		String fileNames[] = new String[0];
		StringBuilder sb = new StringBuilder(FILE_DIR).append(File.separator)
				.append(orderNumber).append(File.separator).append(type);
		File tmpFile = new File(sb.toString());
		if (tmpFile.exists()) {
			fileNames = tmpFile.list();
		}
		return fileNames;
	}

	public void removeFile() throws IOException {
		boolean flag = false;
		StringBuilder sb = new StringBuilder(FILE_DIR).append(File.separator)
				.append(orderNumber).append(File.separator).append(type)
				.append(File.separator).append(fileName);
		File tmpFile = new File(sb.toString());
		if (tmpFile.exists()) {
			tmpFile.setWritable(true);
			flag = tmpFile.delete();
		}
		writeJSON(flag);
	}

	public String download() throws Exception {
		StringBuilder sb = new StringBuilder(FILE_DIR).append(File.separator)
				.append(orderNumber).append(File.separator).append(type)
				.append(File.separator)
				.append(new String(fileName.getBytes("iso8859-1"), "UTF-8"));
		fileInputStream = new FileInputStream(new File(sb.toString()));
		return SUCCESS;
	}

	public String extImages() throws FileNotFoundException {
		StringBuilder sb = new StringBuilder(FILE_DIR).append(File.separator)
				.append(orderNumber).append(File.separator).append(type)
				.append(File.separator).append(fileName);
		inputStream = new FileInputStream(new File(sb.toString()));
		return SUCCESS;
	}
}
